package com.newdjk.newdoctor;

import android.content.Context;
import android.content.pm.ActivityInfo;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.ColorInt;
import androidx.annotation.DrawableRes;
import androidx.annotation.FloatRange;
import androidx.annotation.LayoutRes;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.newdjk.newdoctor.base.BaseAppcompatActivity;

import butterknife.ButterKnife;


public abstract class BasActivity extends BaseAppcompatActivity {
    /**
     * fragment顶部的title
     */
    @Nullable
    private TextView tvTitle;
    private TextView tvLeft;

    /**
     * fragment顶部左边的返回按扭
     */
    @Nullable
    private ImageView ivLeft;
    @Nullable
    private ImageView ivRight;
    @Nullable
    private TextView tvRight;
    private FrameLayout actionbar;

    @Override
    public void setContentView(@LayoutRes int layoutResID) {
        super.setContentView(R.layout.activity_base_nav2);
        LinearLayout root = findViewById(R.id.root_view);
        actionbar = findViewById(R.id.action_bar_container);
        View view = getLayoutInflater().inflate(layoutResID, root, false);
        setInnerContentView(view, null);
    }


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(initViewResId());
        // setDarkStatusWhite(false);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        ButterKnife.bind(this);
        initView();
        initListener();
        initData();
        setNavTitle();
    }

    /**
     * 状态栏白底黑字
     */
    protected void setDarkStatusWhite(boolean bDark) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            View decorView = getWindow().getDecorView();
            getWindow().setStatusBarColor(getResources().getColor(R.color.colorPrimary));//这里对应的是状态栏的颜色，就是style中colorPrimaryDark的颜色
            if (decorView != null) {
                int vis = decorView.getSystemUiVisibility();
                if (bDark) {
                    vis |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
                } else {
                    vis &= ~View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
                }
                decorView.setSystemUiVisibility(vis);
            }
        }
    }

    @Override
    public void setContentView(View view) {
        super.setContentView(R.layout.activity_base_nav2);
        setInnerContentView(view, null);
    }

    @Override
    public void setContentView(View view, ViewGroup.LayoutParams params) {
        super.setContentView(R.layout.activity_base_nav2);
        setInnerContentView(view, params);
    }

    private void setInnerContentView(View view, ViewGroup.LayoutParams layoutParams) {
        try {
            LinearLayout root = findViewById(R.id.root_view);
            if (layoutParams != null) {
                root.addView(view, layoutParams);
            } else {
                if (view != null) {
                    if (root!=null){
                        root.addView(view);
                    }

                }

            }

            findViewById(R.id.tv_left).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onLeftTvClick(v);
                }
            });

            findViewById(R.id.iv_left).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onLeftIvClick(v);
                }
            });

            findViewById(R.id.tv_right).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onRightTvClick(v);
                }
            });

            findViewById(R.id.iv_right).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onRightIvClick(v);
                }
            });
            setLeftIvVisible();
        }catch (Exception e){

        }

    }

    protected void onLeftTvClick(View v) {

    }

    protected void onLeftIvClick(View view) {
        finish();
    }

    protected void onRightTvClick(View v) {

    }

    protected void onRightIvClick(View v) {

    }


    protected void setRightIvVisible(boolean visible) {
        ImageView imageView = findViewById(R.id.iv_right);
        imageView.setVisibility(visible ? View.VISIBLE : View.GONE);
    }

    protected void setRightText(String text) {
        TextView tvritht = findViewById(R.id.tv_right);

        if (TextUtils.isEmpty(text)) {
            tvritht.setVisibility(View.GONE);
        } else {
            tvritht.setVisibility(View.VISIBLE);
            tvritht.setText(text);
        }

    }


    protected void setRightTvVisible(boolean visible) {
        TextView textView = findViewById(R.id.tv_right);
        textView.setVisibility(visible ? View.VISIBLE : View.GONE);
    }

    protected void setLeftIvVisible() {
        ImageView imageView = findViewById(R.id.iv_left);
        imageView.setVisibility(IsShowBackIcon() ? View.VISIBLE : View.GONE);
    }

    public boolean IsShowBackIcon() {
        return false;
    }

    protected void setLeftTvVisible(int visible) {
        findViewById(R.id.tv_left).setVisibility(visible);
    }


    protected void setNavTitle() {
        TextView textView = findViewById(R.id.tv_center_title);
        textView.setText(setTitle());
    }

    protected void setNavTitle(String title) {
        TextView textView = findViewById(R.id.tv_center_title);
        if (title.equals("我的")||title.equals("工作台")) {
            actionbar.setVisibility(View.GONE);
            setStatusBarColor(this, R.color.transparent);

        } else {
            actionbar.setVisibility(View.VISIBLE);
            setStatusBarColor(this, R.color.colorPrimary);
        }
        textView.setText(title);
    }

    protected abstract String setTitle();

    protected void setNavColor(@ColorInt int color) {
        findViewById(R.id.action_bar_container).setBackgroundColor(color);

    }

    protected void setNavAlpha(@FloatRange(from = 0.0, to = 1.0) float alpha) {
        findViewById(R.id.action_bar_container).setAlpha(alpha);
    }


    protected void setToolBarVisiable(boolean visiable) {
        if (visiable) {
            findViewById(R.id.action_bar_container).setVisibility(View.VISIBLE);
        } else {
            findViewById(R.id.action_bar_container).setVisibility(View.GONE);
        }

    }


    protected void setRightTvText(String text) {
        TextView textView = findViewById(R.id.tv_right);
        textView.setText(text);
    }

    protected void setRightIvImage(@DrawableRes int resId) {
        ImageView imageView = findViewById(R.id.iv_right);
        imageView.setImageResource(resId);
    }

    protected void setRightTvTextColor(ColorStateList colorStateList) {
        TextView textView = findViewById(R.id.tv_right);
        textView.setTextColor(colorStateList);
    }

    protected void setRightTvTextColor(int color) {
        TextView textView = findViewById(R.id.tv_right);
        textView.setTextColor(color);
    }

    protected void setLeftTvText(String text) {
        TextView textView = findViewById(R.id.tv_left);
        textView.setText(text);
    }

    protected void setLeftIvImage(@DrawableRes int resId) {
        ImageView imageView = findViewById(R.id.iv_left);
        imageView.setImageResource(resId);
    }

    protected void setLeftTvTextColor(ColorStateList colorStateList) {
        TextView textView = findViewById(R.id.tv_left);
        textView.setTextColor(colorStateList);
    }

    protected void setLeftTvTextColor(int color) {
        TextView textView = findViewById(R.id.tv_left);
        textView.setTextColor(color);
    }

    protected TextView getRightTvText() {
        return findViewById(R.id.tv_right);
    }


    /**
     * 初始化界面
     */
    protected abstract int initViewResId();


    /**
     * 初始化界面
     */
    protected abstract void initListener();

    /**
     * 初始化数据
     */
    protected abstract void initData();

    /**
     * 初始化数据
     */
    protected abstract void initView();

}
